package cn.lixuan.controller;

import cn.lixuan.bo.CommentBO;
import cn.lixuan.constants.BaseInfoProperties;
import cn.lixuan.grace.result.GraceJSONResult;
import cn.lixuan.service.CommentService;
import cn.lixuan.utils.RedisOperator;
import cn.lixuan.vo.CommentVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

@RestController
@Slf4j
@Api(tags = "评论模块接口")
@RequestMapping("comment")
public class CommentController {

    @Autowired
    private CommentService commentService;

    @Autowired
    private RedisOperator redis;

    @ApiOperation(value = "添加评论")
    @PostMapping("create")
    public GraceJSONResult create(@RequestBody @Valid CommentBO commentBO) {

        CommentVO comment = commentService.createComment(commentBO);

        return GraceJSONResult.ok(comment);
    }

    @ApiOperation(value = "获取评论数量")
    @GetMapping("counts")
    public GraceJSONResult counts(@RequestParam String vlogId) {

        String countsStr = redis.get(BaseInfoProperties.REDIS_VLOG_COMMENT_COUNTS + vlogId);
        if (StringUtils.isBlank(countsStr)) {
            countsStr = "0";
        }

        return GraceJSONResult.ok(Integer.valueOf(countsStr));
    }

    @ApiOperation(value = "获取评论信息")
    @GetMapping("list")
    public GraceJSONResult list(@RequestParam String vlogId,
                                @RequestParam(defaultValue = "") String userId,
                                @RequestParam Integer page,
                                @RequestParam Integer pageSize) {

        return GraceJSONResult.ok(commentService.queryVlogComments(vlogId, userId, page, pageSize));
    }

    @ApiOperation(value = "删除评论")
    @DeleteMapping("delete")
    public GraceJSONResult delete(
            @RequestParam String commentUserId,
            @RequestParam String commentId,
            @RequestParam String vlogId
    ) {
        commentService.deleteComment(commentUserId, commentId, vlogId);

        return GraceJSONResult.ok();
    }

    @ApiOperation(value = "对评论点赞")
    @PostMapping("like")
    public GraceJSONResult like(@RequestParam String userId, @RequestParam String commentId) {

        redis.incrementHash(BaseInfoProperties.REDIS_VLOG_COMMENT_LIKED_COUNTS, commentId, 1);
        redis.setHashValue(BaseInfoProperties.REDIS_USER_LIKE_COMMENT, userId + ":" + commentId, "1");

        return GraceJSONResult.ok();
    }

    @ApiOperation(value = "取消对评论的点赞")
    @PostMapping("unlike")
    public GraceJSONResult unlike(@RequestParam String userId, @RequestParam String commentId) {

        redis.decrementHash(BaseInfoProperties.REDIS_VLOG_COMMENT_LIKED_COUNTS, commentId, 1);
        redis.hdel(BaseInfoProperties.REDIS_USER_LIKE_COMMENT, userId + ":" + commentId);

        return GraceJSONResult.ok();
    }
}
